package com.execjar;

import cn.hutool.core.thread.ThreadUtil;
import com.execjar.fun.FunctionParse;
import com.execjar.fun.FunctionPound;
import com.execjar.utils.TcpToolUtils;
import com.google.common.collect.EvictingQueue;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
public class App {

    private static final int LIST_SIZE = 5;


    public static void main1(String[] args) {
        // 5302  |192.168.50.62
        EvictingQueue<Integer> queue = EvictingQueue.create(LIST_SIZE);

        if (StringUtils.equals("b", args[1])) {
            for (; ; ) {
                try {
                    ThreadUtil.safeSleep(1500);
                    byte[] tcpRead = TcpToolUtils.postReAssignByte(args[0], 5302, 50);
                    Integer readPound = funParseRead.apply(tcpRead);
                    System.out.println("读取结果=" + readPound);

                    queue.add(readPound);
                    System.out.println(String.format("当前队列大小：%d，队列中元素：%s", queue.size(), StringUtils.join(queue.iterator(), ',')));

                } catch (Exception e) {
                    System.out.println("读取异常");
                }

            }
        } else {

            for (; ; ) {
                try {
                    ThreadUtil.safeSleep(1500);
                    byte[] tcpRead = TcpToolUtils.postReAssignByte(args[0], 5302, 50);
                    Integer readPound = funParseRead.apply(tcpRead);
                    System.out.println("读取结果=" + readPound);

                    queue.add(readPound);
                    System.out.println(String.format("当前队列大小：%d，队列中元素：%s", queue.size(), StringUtils.join(queue.iterator(), ',')));


                    if (queue.size() < LIST_SIZE) {
                        continue;
                    }
                    List<Integer> collect = queue.stream().distinct().collect(Collectors.toList());
                    if (collect.size() == 1) {
                        System.out.println("称重结束，结果为=" + collect.get(0));
                        break;
                    }

                } catch (Exception e) {
                    System.out.println("读取异常");
                }

            }
        }
    }


    static FunctionParse<byte[], Integer> funParseRead = (readBytes) -> Optional.ofNullable(readBytes)
            .map(item -> {
                String s = new String(item, StandardCharsets.UTF_8);
                log.info("地磅读取返回[{}]", s);
                return s;
            }).filter(item -> item.length() > 36).map(item -> {
                List<String> collect = Arrays.stream(item.split("\u0003\u0002"))
                        .filter(str -> StringUtils.length(str.trim()) >= 9)
                        .map(str -> StringUtils.substring(str, 2, str.length() - 3))
                        .collect(Collectors.toList());
                //读取到的不是标准，截取2位的第二位
                String parseIntStr = collect.size() >= 2 ? collect.get(1) : "";
                return Integer.parseInt(parseIntStr);
            }).get();
    static FunctionPound<String, String, Integer, byte[]> funcPound = (ip, port, readLen) -> TcpToolUtils.postReAssignByte(ip, Integer.parseInt(port), readLen);


}
